﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace YJKExcelFormTool.Forms
{
	public partial class ExportExcelSettingForm : System.Windows.Forms.Form
	{
		public int EndRowInfo
		{
			get
			{
				return this.m_EndRowInfo.EndRowNumber;
			}
		}

		public bool UsingDefaultFontSetting
		{
			get
			{
				return this.m_UsingDefaultFontSetting;
			}
		}

		public string FontName
		{
			get
			{
				return this.m_FontName;
			}
		}

		public int FontSize
		{
			get
			{
				return this.m_FontSize;
			}
		}

		public int FontHorizontalalignment
		{
			get
			{
				return this.m_FontHorizontalAlignment;
			}
		}

		public int FontVerticalAlignment
		{
			get
			{
				return this.m_FontVerticalAlignment;
			}
		}

		public ExportExcelSettingForm(ExportExcelSettingForm.ExportType exportType, string fontName, int fontSize)
		{
			this.m_ExportType = exportType;
			this.m_FontName = fontName;
			this.m_FontSize = fontSize;
			this.InitializeComponent();
			this.InitFont();
		}

		public void SetPageRowInfo(int headRowsCount, int endRowNumber)
		{
			this.m_EndRowInfo.EndRowNumber = endRowNumber;
			this.m_EndRowInfo.HeaderRowCount = headRowsCount;
			if (endRowNumber <= headRowsCount)
			{
				this.m_EndRowInfo.EndRowNumber = headRowsCount + 36;
			}
		}

		private void ExportExcelSettingForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.OK)
			{
				this.m_UsingDefaultFontSetting = this.chekUsingDefaultFontSetting.Checked;
				this.m_FontName = this.cmbFontName.Text;
				this.m_FontSize = Convert.ToInt32(this.cmbFontSize.Text);
				this.m_FontHorizontalAlignment = this.cmbHAlignment.SelectedIndex;
				this.m_FontVerticalAlignment = this.cmbVAlignment.SelectedIndex;
				this.SaveCompornentData();
			}
		}

		private void ExportExcelSettingForm_Load(object sender, EventArgs e)
		{
			this.tbTableRowCount.Text = this.m_EndRowInfo.HeaderRowCount.ToString();
			this.nudEndNumb.Value = this.m_EndRowInfo.EndRowNumber;
			this.LoadCompornentData();
		}

		private void nudEndNumb_ValueChanged(object sender, EventArgs e)
		{
			int num = Convert.ToInt32(this.nudEndNumb.Value);
			if (num <= this.m_EndRowInfo.HeaderRowCount || num > 100)
			{
				this.nudEndNumb.Value = this.m_EndRowInfo.EndRowNumber;
				return;
			}
			this.m_EndRowInfo.EndRowNumber = num;
		}

		private void chekUsingDefaultFontSetting_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chekUsingDefaultFontSetting.Checked)
			{
				this.cmbFontName.Enabled = false;
				this.cmbFontSize.Enabled = false;
				this.cmbHAlignment.Enabled = false;
				this.cmbVAlignment.Enabled = false;
				return;
			}
			this.cmbFontName.Enabled = true;
			this.cmbFontSize.Enabled = true;
			this.cmbHAlignment.Enabled = true;
			this.cmbVAlignment.Enabled = true;
		}

		private int CalculateEndRowNum(Worksheet workSheet, ref bool hasPrintArea)
		{
			int result = 36;
			hasPrintArea = false;
			string printArea = workSheet.PageSetup.PrintArea;
			if (printArea == null)
			{
				return result;
			}
			int num = printArea.LastIndexOf("$");
			if (num > -1)
			{
				result = Convert.ToInt32(printArea.Substring(num + 1, printArea.Length - num - 1));
				hasPrintArea = true;
			}
			return result;
		}

		private void InitFont()
		{
			FontFamily[] families = new InstalledFontCollection().Families;
			int selectedIndex = 0;
			int num = families.Length;
			for (int i = 0; i < num; i++)
			{
				string name = families[i].Name;
				this.cmbFontName.Items.Add(name);
				if (this.m_FontName.IndexOf(name) >= 0)
				{
					selectedIndex = i;
				}
			}
			if (num > 0)
			{
				this.cmbFontName.SelectedIndex = selectedIndex;
			}
			int selectedIndex2 = 0;
			int num2 = 1000;
			for (int i = 0; i < this.cmbFontSize.Items.Count; i++)
			{
				int num3 = Convert.ToInt32(this.cmbFontSize.Items[i].ToString());
				int num4 = Math.Abs(this.m_FontSize - num3);
				if (num4 < num2)
				{
					num2 = num4;
					selectedIndex2 = i;
				}
			}
			this.cmbFontSize.SelectedIndex = selectedIndex2;
			this.cmbHAlignment.SelectedIndex = 1;
			this.cmbVAlignment.SelectedIndex = 1;
			this.chekUsingDefaultFontSetting.Checked = true;
			this.cmbFontName.Enabled = false;
			this.cmbFontSize.Enabled = false;
			this.cmbHAlignment.Enabled = false;
			this.cmbVAlignment.Enabled = false;
		}

		[DllImport("kernel32.dll")]
		public static extern IntPtr _lopen(string lpPathName, int iReadWrite);

		[DllImport("kernel32.dll")]
		public static extern bool CloseHandle(IntPtr hObject);

		private string GetSettingFileName()
		{
			string result = "";
			switch (this.m_ExportType)
			{
			case ExportExcelSettingForm.ExportType.GraphPaperDirectory:
				result = "DExportExcelSettingForm.dat";
				break;
			case ExportExcelSettingForm.ExportType.Electric:
				result = "EExportExcelSettingForm.dat";
				break;
			case ExportExcelSettingForm.ExportType.HVAC:
				result = "HExportExcelSettingForm.dat";
				break;
			case ExportExcelSettingForm.ExportType.Plumbing:
				result = "PExportExcelSettingForm.dat";
				break;
			case ExportExcelSettingForm.ExportType.DoorWindowDetailedStatement:
				result = "DWExportExcelSettingForm.dat";
				break;
			case ExportExcelSettingForm.ExportType.GeneralTable:
				result = "GTExportExcelSettingForm.dat";
				break;
			}
			return result;
		}

		private void LoadCompornentData()
		{
			string path = Path.GetTempPath() + this.GetSettingFileName();
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			this.m_UsingDefaultFontSetting = Convert.ToBoolean(streamReader.ReadLine());
			this.chekUsingDefaultFontSetting.Checked = this.m_UsingDefaultFontSetting;
			int num = Convert.ToInt32(streamReader.ReadLine());
			if (num < this.cmbHAlignment.Items.Count)
			{
				this.cmbHAlignment.SelectedIndex = num;
			}
			int num2 = Convert.ToInt32(streamReader.ReadLine());
			if (num2 < this.cmbVAlignment.Items.Count)
			{
				this.cmbVAlignment.SelectedIndex = num2;
			}
			this.nudEndNumb.Value = Convert.ToInt32(streamReader.ReadLine());
			this.m_EndRowInfo.EndRowNumber = (int)this.nudEndNumb.Value;
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveCompornentData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + this.GetSettingFileName(), FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.m_UsingDefaultFontSetting);
			streamWriter.WriteLine(this.m_FontHorizontalAlignment);
			streamWriter.WriteLine(this.m_FontVerticalAlignment);
			streamWriter.WriteLine(this.EndRowInfo);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void nudEndNumb_Validating(object sender, CancelEventArgs e)
		{
			try
			{
				int num = Convert.ToInt32(this.nudEndNumb.Value.ToString());
				if (num < 1 || num > 1000)
				{
					if (this.m_EndRowInfo.EndRowNumber > 0 && this.m_EndRowInfo.EndRowNumber <= 1000)
					{
						this.nudEndNumb.Value = this.m_EndRowInfo.EndRowNumber;
					}
					else
					{
						this.nudEndNumb.Value = 10m;
					}
				}
			}
			catch (FormatException)
			{
				if (this.m_EndRowInfo.EndRowNumber > 0 && this.m_EndRowInfo.EndRowNumber <= 1000)
				{
					this.nudEndNumb.Value = this.m_EndRowInfo.EndRowNumber;
				}
				else
				{
					this.nudEndNumb.Value = 10m;
				}
			}
		}

		private ExportExcelSettingForm.MaxRowInfoWithWorkSheet m_EndRowInfo;

		private bool m_UsingDefaultFontSetting;

		private string m_FontName;

		private int m_FontSize;

		private int m_FontHorizontalAlignment;

		private int m_FontVerticalAlignment;

		private ExportExcelSettingForm.ExportType m_ExportType;

		public const int OF_READWRITE = 2;

		public const int OF_SHARE_DENY_NONE = 64;

		public readonly IntPtr HFILE_ERROR = new IntPtr(-1);

		public enum ExportType
		{
			GraphPaperDirectory,
			Electric,
			HVAC,
			Plumbing,
			DoorWindowDetailedStatement,
			GeneralTable
		}

		private struct MaxRowInfoWithWorkSheet
		{
			public int EndRowNumber;

			public int HeaderRowCount;
		}
	}
}
